<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索区域 -->
      <div class="filter-container">
        <el-input
          v-model="listQuery.search"
          placeholder="请输入用户昵称/手机号"
          clearable
          style="width: 200px"
          @keyup.enter="handleFilter"
        />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
      </div>

      <!-- 表格区域 -->
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        stripe
        style="width: 100%; margin-top: 10px"
      >
        <el-table-column type="index" width="50" label="序号" align="center" />
        <el-table-column prop="id" label="提现ID" width="120" align="center" />
        <el-table-column prop="nickname" label="昵称" width="120" />
        <el-table-column prop="mobile" label="手机号" width="120" />
        <el-table-column prop="amount" label="提现金额" width="100" />
        <el-table-column prop="account" label="支付宝账号" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.status === 'pending'
                  ? 'warning'
                  : scope.row.status === 'approved'
                  ? 'success'
                  : 'danger'
              "
            >
              {{ statusMap[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" width="160">
          <template #default="scope">
            {{ formatDate(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180" align="center">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === 'pending'"
              size="small"
              type="success"
              @click="handleApprove(scope.row)"
              >同意</el-button
            >
            <el-button
              v-if="scope.row.status === 'pending'"
              size="small"
              type="danger"
              @click="handleReject(scope.row)"
              >拒绝</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="listQuery.page"
          :page-size="listQuery.per_page"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getWithdrawList, statusWithdraw } from "@/api/commission";

const list = ref([]);
const total = ref(0);
const listLoading = ref(false);

const listQuery = reactive({
  page: 1,
  per_page: 20,
  search: "",
});

const statusMap = {
  pending: "待审核",
  approved: "已通过",
  rejected: "已拒绝",
};

// 获取列表
const getList = async () => {
  listLoading.value = true;
  try {
    const res = await getWithdrawList(listQuery);
    list.value = res.data.items || [];
    total.value = res.data.total || 0;
  } catch (err) {
    console.error("获取提现列表失败", err);
    ElMessage.error("获取提现列表失败");
  } finally {
    listLoading.value = false;
  }
};

// 搜索
const handleFilter = () => {
  listQuery.page = 1;
  getList();
};

// 分页
const handleSizeChange = (val) => {
  listQuery.per_page = val;
  getList();
};
const handleCurrentChange = (val) => {
  listQuery.page = val;
  getList();
};
// 同意提现
const handleApprove = (row) => {
  ElMessageBox.confirm(
    `确认同意用户 ${row.nickname} 提现 ¥${row.amount} 吗？`,
    "提示",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(async () => {
    try {
      await statusWithdraw({
        id: row.id,
        status: "approved", // 可以根据实际情况修改拒绝理由
      });
      ElMessage.success("提现已同意");
      getList();
    } catch (error) {
      console.error("提现同意失败", error);
      ElMessage.error("操作失败");
    }
  });
};

// 拒绝提现
const handleReject = (row) => {
  ElMessageBox.confirm(`确认拒绝用户 ${row.nickname} 的提现申请？`, "提示", {
    confirmButtonText: "拒绝",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    try {
      await statusWithdraw({
        id: row.id,
        status: "rejected", // 可以根据实际情况修改拒绝理由
      });
      ElMessage.success("提现已拒绝");
      getList();
    } catch (error) {
      console.error("提现拒绝失败", error);
      ElMessage.error("操作失败");
    }
  });
};

// 日期格式化
const formatDate = (dateStr) => {
  if (!dateStr) return "";
  const date = new Date(dateStr);
  return date.toLocaleString();
};

onMounted(() => {
  getList();
});
</script>
